package com.yer.note.activity;


import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.yer.note.R;
import com.yer.note.entity.Diary;
import com.yer.note.entity.User;
import com.yer.note.utils.BaseUtils;

import java.util.List;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Boolean bPwdSwitch = false;
    private EditText etPwd;
    private EditText etAccount;
    private CheckBox cbRememberPwd;
    private String account;
    private TextView signup;
    List<User> userList;
    ImageView ivPwdSwitch;
    Button btLogin;
    public void initView(){
        signup = findViewById(R.id.tv_sign_up);
        ivPwdSwitch = findViewById(R.id.iv_pwd_switch);
        etPwd = findViewById(R.id.et_pwd);
        etAccount = findViewById(R.id.et_account);
        cbRememberPwd = findViewById(R.id.cb_remember_pwd);
        btLogin = findViewById(R.id.bt_login);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        initView();
        userList = User.listAll(User.class);
        System.out.println(userList);
        if(userList.isEmpty()){
            String news = "该软件尚无用户，仅能注册";
            BaseUtils.shortTipInCoast(LoginActivity.this,news);
            btLogin.setEnabled(false);
        }
        btLogin.setOnClickListener(this);
        signup.setOnClickListener(this);
        //密码是否可见
        ivPwdSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bPwdSwitch = !bPwdSwitch;
                if (bPwdSwitch) {
                    ivPwdSwitch.setImageResource(
                            R.drawable.ic_baseline_visibility_24);
                    etPwd.setInputType(
                            InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    ivPwdSwitch.setImageResource(
                            R.drawable.ic_baseline_visibility_off_24);
                    etPwd.setInputType(
                            InputType.TYPE_TEXT_VARIATION_PASSWORD |
                                    InputType.TYPE_CLASS_TEXT);
                    etPwd.setTypeface(Typeface.DEFAULT);
                }
            }
        });
    }

    /**
     * 判断是不是用户：是则返回用户对应的User否则返回null
     * @param userName
     * @param password
     * @return
     */
    public User isAcount(String userName,String password){
        int flag = 0;
        for (User user : userList) {
            if(user.getName().equals(userName)){
                flag = 1;
                if(user.getPassword().equals(password)){
                    return user;
                }
            }
        }
        if(flag==0){
            String news = "查无此人，请重新注册！";
            BaseUtils.shortTipInCoast(LoginActivity.this,news);
        }else{
            String news = "用户名或密码输入错误";
            BaseUtils.shortTipInCoast(LoginActivity.this,news);
        }
        return null;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_login:{
                String userName = etAccount.getText().toString();
                String password = etPwd.getText().toString();
                User user = isAcount(userName,password);
                if(user!=null){
                    User.user = user;
                    System.out.println("登录一个用户："+user);
                    Intent intent = new Intent(LoginActivity.this,NoteActivity.class);
                    startActivity(intent);
                }
                break;
            }
            case R.id.tv_sign_up:{
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        }
    }
}